package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.domain.Client;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.io.IOException;
import java.io.Reader;

/**
 *
 * @author Aarón
 */
public class ClientDaoManager extends DAOManager {

    protected static ClientDaoManager instance = null;
    private ClientDAO clientDAO = null;

    private ClientDaoManager() throws IOException {
        super();
        Reader readerDao = Resources.getResourceAsReader("com/eupmt/lab3/persistence/ibatis/daoConfig.xml");
        DaoManager daoManager = DaoManagerBuilder.buildDaoManager(readerDao);
        this.clientDAO = (ClientDAO) daoManager.getDao(ClientDAO.class);
//        clientDAO = new ClientDAOImpl();
    }

    // Metodes ClientDaoManager

    public Client getClientById(int idClient) throws Exception {
        return clientDAO.getClientById(idClient);
    }

    public Client getClientByNif(String nif) throws Exception {
        return clientDAO.getClientByNif(nif);
    }

    public void insertClient(Client client) throws Exception {
        clientDAO.insertClient(client);
    }

    public void updateClient(Client client) throws Exception {
        clientDAO.updateClient(client);
    }

    public void deleteClient(Client client) throws Exception {
        clientDAO.deleteClient(client);
    }

    public void saveClient(Client client) throws Exception {
        clientDAO.saveClient(client);
    }

    public static ClientDaoManager getInstance() throws IOException {
        if (instance == null) {
            instance = new ClientDaoManager();
        }
        return instance;
    }
}
